package com.ssm.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.validator.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ssm.mapper.MaterialMapper;
import com.ssm.mapper.TeacherMapper;
import com.ssm.model.MaterialTemplateModel;
import com.ssm.model.TeacherMaterialModel;
import com.ssm.model.TeacherSummaryModel;
import com.ssm.service.MaterialService;

@Service
public class MaterialServiceImpl implements MaterialService {
	
	@Autowired
	private MaterialMapper materialMapper;
	@Autowired
	private TeacherMapper teacherMapper;

	@Override
	public List selectMaterialTemplate() {
		// TODO Auto-generated method stub
		MaterialTemplateModel materialTemplateModel = materialMapper.selectMaterialTemplate();
		String mtDesc = materialTemplateModel.getMtDesc();
		//String-->map
		JSONObject jsonObject = JSONObject.parseObject(mtDesc);
		Map map = (Map)jsonObject;
		List list = (List) map.get("root");
		System.out.println("list-->"+list.toString());
		
		/*HashMap map2 = new HashMap<>();
		map2.put("tmContent", list.toString());
		materialMapper.insertTeacherMaterial(map2);*/
		return list;
	}

	@Override
	public Integer updateMaterialTemplate(List dataList) {
		// TODO Auto-generated method stub
		Map map = new HashMap<>();
		map.put("root", dataList);
		String jsonString = JSON.toJSONString(map);
		Map paramMap = new HashMap<>();
		paramMap.put("mtDesc", jsonString);
		int i = materialMapper.updateMaterialTemplate(paramMap);
		return i;
	}

	@Override
	public List selectTeacherMaterial(Integer tId) {
		// TODO Auto-generated method stub
		TeacherMaterialModel teacherMaterialModel = materialMapper.selectTeacherMaterial(tId);
		String teacherMaterialString = teacherMaterialModel.getTmContent();
		//String-->map
		JSONObject jsonObject = JSONObject.parseObject(teacherMaterialString);
		Map tempMap = (Map)jsonObject;
		List returnList = (List) tempMap.get("root");
		return returnList;
	}

	@Override
	public Integer updateTeacherMaterial(Integer tId,List materialList) {
		// TODO Auto-generated method stub
		Map map = new HashMap<>();
		map.put("root", materialList);
		String jsonString = JSON.toJSONString(map);
		Map paramMap = new HashMap<>();
		paramMap.put("tmContent", jsonString);
		paramMap.put("tId", tId);
		Integer i = materialMapper.updateTeacherMaterial(paramMap);
		return i;
	}

	@Override
	public List selecTeacherSummaryByTid(Integer tId) {
		// TODO Auto-generated method stub
		TeacherSummaryModel teacherSummaryModel = materialMapper.selectTeacherSummaryByTid(tId);
		if(teacherSummaryModel!=null){
			String tsContent = teacherSummaryModel.getTsContent();
			//String-->map
			JSONObject jsonObject = JSONObject.parseObject(tsContent);
			Map map = (Map)jsonObject;
			List list = (List) map.get("root");
			return list;
		}else{
			return null;
		}
	}

	@Override
	public Integer insertTeacherSummaryByTid(Map paramMap) {
		// TODO Auto-generated method stub
		Integer tId = (Integer)paramMap.get("tId");
		Integer integer = materialMapper.insertTeacherSummaryByTid(paramMap);
		if(integer!=0){
			//将tsId更新到assessplan中
			TeacherSummaryModel teacherSummaryModel = materialMapper.selectTeacherSummaryByTid(tId);
			HashMap map = new HashMap<>();
			map.put("tId", tId);
			map.put("tsId", teacherSummaryModel.getTsId());
			teacherMapper.updateTsIdOnPlan(map);
		}
		return integer;
	}

	@Override
	public Integer updateTeacherSummaryByTid(Map paramMap) {
		// TODO Auto-generated method stub
		return materialMapper.updateTeacherSummaryByTid(paramMap);
	}
	
	
	
	
	

}
